package com.ikas.autumn.iec104.module.iec.asdunew.internal;

import java.util.HashMap;
import java.util.Map;

/**
 * Every ASDU contains a single Cause Of Transmission field so the recipient knows why the message it received was sent.
 * Parts IEC 60870-5-101 and IEC 60870-5-104 define what CauseOfTransmissions are allowed for the different ASDU types.
 * CauseOfTransmissions 44 to 47 are meant for replies to commands with undefined values.
 * @author wu.yunfeng
 */
public enum CauseOfTransmission {
    /**
     * 传输原因序号   1-6位
     * 3：突发，自发
     * 4：初始化
     * 6：激活
     * 7：激活确认
     * 8：停止激活
     * 9：停止激活确认
     * 10：激活终止
     * 20：响应站召唤
     */
    /** PERIODIC(1) */
    PERIODIC(1, "定期"),
    /** BACKGROUND_SCAN(2) */
    BACKGROUND_SCAN(2, "背景扫描"),
    /** SPONTANEOUS(3) */
    SPONTANEOUS(3, "突发，自发"),
    /** INITIALIZED(4) */
    INITIALIZED(4, "初始化"),
    /** REQUEST(5) */
    REQUEST(5, "请求"),
    /** ACTIVATION(6) */
    ACTIVATION(6, "激活"),
    /** ACTIVATION_CON(7) */
    ACTIVATION_CON(7, "激活确认"),
    /** DEACTIVATION(8) */
    DEACTIVATION(8, "停止激活"),
    /** DEACTIVATION_CON(9) */
    DEACTIVATION_CON(9, "停止激活确认"),
    /** ACTIVATION_TERMINATION(10) */
    ACTIVATION_TERMINATION(10, "激活终止"),
    /** RETURN_INFO_REMOTE(11) */
    RETURN_INFO_REMOTE(11, "返回远程信息"),
    /** RETURN_INFO_LOCAL(12) */
    RETURN_INFO_LOCAL(12, "返回本地信息"),
    /** FILE_TRANSFER(13) */
    FILE_TRANSFER(13, "文件传输"),
    /** INTERROGATED_BY_STATION(20) */
    INTERROGATED_BY_STATION(20, "响应站召唤"),
    /** INTERROGATED_BY_GROUP_1(21) */
    INTERROGATED_BY_GROUP_1(21, "组1召唤"),
    /** INTERROGATED_BY_GROUP_2(22) */
    INTERROGATED_BY_GROUP_2(22, "组2召唤"),
    /** INTERROGATED_BY_GROUP_3(23) */
    INTERROGATED_BY_GROUP_3(23, "组3召唤"),
    /** INTERROGATED_BY_GROUP_4(24) */
    INTERROGATED_BY_GROUP_4(24, "组4召唤"),
    /** INTERROGATED_BY_GROUP_5(25) */
    INTERROGATED_BY_GROUP_5(25, "组5召唤"),
    /** INTERROGATED_BY_GROUP_6(26) */
    INTERROGATED_BY_GROUP_6(26, "组6召唤"),
    /** INTERROGATED_BY_GROUP_7(27) */
    INTERROGATED_BY_GROUP_7(27, "组7召唤"),
    /** INTERROGATED_BY_GROUP_7(27) */
    INTERROGATED_BY_GROUP_8(28, "组7召唤"),
    /** INTERROGATED_BY_GROUP_8(28) */
    INTERROGATED_BY_GROUP_9(29, "组9召唤"),
    /** INTERROGATED_BY_GROUP_9(29) */
    INTERROGATED_BY_GROUP_10(30, "组10召唤"),
    /** INTERROGATED_BY_GROUP_11(31) */
    INTERROGATED_BY_GROUP_11(31, "组11召唤"),
    /** INTERROGATED_BY_GROUP_12(32) */
    INTERROGATED_BY_GROUP_12(32, "组12召唤"),
    /** INTERROGATED_BY_GROUP_13(33) */
    INTERROGATED_BY_GROUP_13(33, "组13召唤"),
    /** INTERROGATED_BY_GROUP_14(34) */
    INTERROGATED_BY_GROUP_14(34, "组14召唤"),
    /** INTERROGATED_BY_GROUP_15(35) */
    INTERROGATED_BY_GROUP_15(35, "组15召唤"),
    /** INTERROGATED_BY_GROUP_16(36) */
    INTERROGATED_BY_GROUP_16(36, "组16召唤"),
    /** REQUESTED_BY_GENERAL_COUNTER(37) */
    REQUESTED_BY_GENERAL_COUNTER(37, "普通请求统计"),
    /** REQUESTED_BY_GENERAL_COUNTER(37) */
    REQUESTED_BY_GROUP_1_COUNTER(38, "组1请求统计"),
    /** REQUESTED_BY_GROUP_2_COUNTER(39) */
    REQUESTED_BY_GROUP_2_COUNTER(39, "组2请求统计"),
    /** REQUESTED_BY_GROUP_3_COUNTER(40) */
    REQUESTED_BY_GROUP_3_COUNTER(40, "组3请求统计"),
    /** REQUESTED_BY_GROUP_4_COUNTER(41), */
    REQUESTED_BY_GROUP_4_COUNTER(41, "组4请求统计"),
    /** UNKNOWN_TYPE_ID(44) */
    UNKNOWN_TYPE_ID(44, "未知类型id"),
    /** UNKNOWN_CAUSE_OF_TRANSMISSION(45) */
    UNKNOWN_CAUSE_OF_TRANSMISSION(45,"传送原因未知"),
    /** UNKNOWN_COMMON_ADDRESS_OF_ASDU(46) */
    UNKNOWN_COMMON_ADDRESS_OF_ASDU(46, "asdu的公共地址未知"),
    /** UNKNOWN_INFORMATION_OBJECT_ADDRESS(47) */
    UNKNOWN_INFORMATION_OBJECT_ADDRESS(47, "未知信息对象地址");

    private final int id;

    private final String desc;

    private static final Map<Integer, CauseOfTransmission> idMap = new HashMap<>();

    static {
        for (CauseOfTransmission enumInstance : CauseOfTransmission.values()) {
            if (idMap.put(enumInstance.getId(), enumInstance) != null) {
                throw new IllegalArgumentException("duplicate ID: " + enumInstance.getId());
            }
        }
    }

    CauseOfTransmission(int id, String desc) {
        this.id = id;
        this.desc = desc;
    }

    /**
     * Returns the ID of this CauseOfTransmission.
     * 
     * @return the ID.
     */
    public int getId() {
        return id;
    }
    /**
     * Returns the desc of this CauseOfTransmission.
     *
     * @return the ID.
     */
    public String getDesc() {
        return desc;
    }

    /**
     * Returns the CauseOfTransmission that corresponds to the given ID. Returns <code>null</code> if no
     * CauseOfTransmission with the given ID exists.
     * 
     * @param id
     *            the ID.
     * @return the CauseOfTransmission that corresponds to the given ID.
     */
    public static CauseOfTransmission causeFor(int id) {
        return idMap.get(id);
    }

}
